//
//  PywBuriedDataHandler.m
//  PywSdk
//
//  Created by Ampaw on 2017/11/15.
//  Copyright © 2017年 Jade. All rights reserved.
//

#import "PywBuriedDataHandler.h"
#import "PywBuriedManager.h"

#define pywUserActionDataPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"pywUserActionDataPath"]

@implementation PywBuriedDataHandler

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self.page_sn        = [aDecoder decodeObjectForKey:@"page_sn"];
    self.action_sn      = [aDecoder decodeObjectForKey:@"action_sn"];
    self.type_sn        = [aDecoder decodeObjectForKey:@"type_sn"];
    self.game_id        = [aDecoder decodeObjectForKey:@"game_id"];
    self.channel_id     = [aDecoder decodeObjectForKey:@"channel_id"];
    self.promo_code     = [aDecoder decodeObjectForKey:@"promo_code"];
    self.promo_channel  = [aDecoder decodeObjectForKey:@"promo_channel"];
    self.action_time    = [aDecoder decodeObjectForKey:@"action_time"];
    self.ip             = [aDecoder decodeObjectForKey:@"ip"];
    self.os             = [aDecoder decodeObjectForKey:@"os"];
    self.imei           = [aDecoder decodeObjectForKey:@"imei"];
    self.account        = [aDecoder decodeObjectForKey:@"account"];
    
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.page_sn forKey:@"page_sn"];
    [aCoder encodeObject:self.action_sn forKey:@"action_sn"];
    [aCoder encodeObject:self.type_sn forKey:@"type_sn"];
    [aCoder encodeObject:self.game_id forKey:@"game_id"];
    [aCoder encodeObject:self.channel_id forKey:@"channel_id"];
    [aCoder encodeObject:self.promo_code forKey:@"promo_code"];
    [aCoder encodeObject:self.promo_channel forKey:@"promo_channel"];
    [aCoder encodeObject:self.action_time forKey:@"action_time"];
    [aCoder encodeObject:self.ip forKey:@"ip"];
    [aCoder encodeObject:self.os forKey:@"os"];
    [aCoder encodeObject:self.imei forKey:@"imei"];
    [aCoder encodeObject:self.account forKey:@"account"];
}

/**
 存储用户行为数据
 
 @param userActionData 用户行为数据
 */
+ (void)saveUserActionDataWithUserActionData:(PywBuriedDataHandler *)userActionData
{
    // 获取已存储的数据
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:pywUserActionDataPath];
    
    // 追加存储数据
    NSMutableArray *userActionDatas = [NSMutableArray arrayWithArray:array];
    [userActionDatas addObject:userActionData];
    
    // 写入文件存储
    [NSKeyedArchiver archiveRootObject:userActionDatas toFile:pywUserActionDataPath];
}

/**
 删除上报成功的用户行为数据
 */
+ (void)removeUserActionData
{
    NSMutableArray *userActionDatas = [NSMutableArray arrayWithArray:[self userActionDataLists]];
    [userActionDatas removeAllObjects];
    // 移除数据后，空数组写入文件
    [NSKeyedArchiver archiveRootObject:userActionDatas toFile:pywUserActionDataPath];
}

/**
 获取用户行为数据数据集
 
 @return 返回：当前设备上，存储的所有用户行为数据
 */
+ (NSMutableArray *)userActionDataLists
{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:pywUserActionDataPath];
}

@end
